package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;

@RabbitListener(queues = {"hello-mq"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queue:声明需要监听的所有队列
     * <p>
     * org.springframework.amqp.core
     * <p>
     * Message: 原生消息详细信息，头和体
     * T<发送的消息的类型> OrderReturnReasonEntity
     * 一个客户端一条连接
     * Channel: 当前传输数据的通道
     * <p>
     * Queue：可以很多人都来监听，只要收到消息，队列删除消息，而且只能有一个收到消息
     * 场景：
     * 1.订单服务启动多个，同一个消息，只能有一个客户端收到
     * 2.只有一个消息完全处理完，方法运行结束，我们就可以接收下一个消息
     */
    @RabbitHandler
    public void recieveMessage(Message msg, OrderReturnReasonEntity content, Channel channel) {
        // (Body:'{"id":1,"name":"朱元璋","sort":null,"status":null,"createTime":1625278877867}'
        byte[] body = msg.getBody();
        // 设置消息头属性信息 MessageProperties [headers={__TypeId__=com.atguigu.gulimall.order.entity.OrderReturnReasonEntity}
        System.out.println("接收到消息：" + msg);
        System.out.println("内容：" + content);
        System.out.println("消息处理完成=>" + content.getName());

        MessageProperties messageProperties = msg.getMessageProperties();
        // channel内按顺序自增的
        long deliveryTag = messageProperties.getDeliveryTag();
        System.out.println("deliveryTag==>" + deliveryTag);
        // 签收货物
        try {
            /*
                long deliveryTag 派发的标签
                boolean multiple
                    false 只签收当前的货物
            */
            if (deliveryTag % 2 == 0) {
                // 收货
                channel.basicAck(deliveryTag, false);
                System.out.println("签收了货物..." + deliveryTag);
            } else {
                // 退货
                /*
                     long deliveryTag 货物标签
                     boolean multiple
                     boolean requeue 是否重新入队mq
                        true 发回服务器，服务器重新入队mq
                        false 丢弃
                 */
                channel.basicNack(deliveryTag, false, true);
                // 拒绝
                /*
                    long deliveryTag, boolean requeue
                 */
                // channel.basicReject();
                System.out.println("没有签收货物..." + deliveryTag);
            }
        } catch (IOException e) {
            // 网络中断
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void recieveMessage2(OrderEntity content) {
        // (Body:'{"id":1,"name":"朱元璋","sort":null,"status":null,"createTime":1625278877867}'
        System.out.println("接收到消息recieveMessage2：" + content);
    }
}